using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using System.Drawing;
using System.Drawing.Imaging;
using System.ComponentModel;
using System.Windows.Forms.VisualStyles;
using IReaper.FileData;

namespace IReaper.CourseData
{
    public class DataGridViewDownloadTaskCell : DataGridViewImageCell
    {
        #region static variable and constructor
        static Bitmap pptImage, codeImage, videoImage,qaImage;
        static Bitmap pptLightImage, codeLightImage, videoLightImage,qaLightImage;
        static Bitmap reminderImage, lightReminderImage;
        static Bitmap completeImage, emptyImage;
        static Graphics g;

        static DataGridViewDownloadTaskCell()
        {
            //load image
            //reminder
            reminderImage = (Bitmap)Properties.Resources.Reminder.GetThumbnailImage(20,20,null,IntPtr.Zero);
            reminderImage.MakeTransparent(Color.White);
            //light reminder
            lightReminderImage = (Bitmap)Properties.Resources.lightReminder.GetThumbnailImage(20, 20, null, IntPtr.Zero);
            lightReminderImage.MakeTransparent(Color.White);
            //orignial
            Image image = (Image)Properties.Resources.ResourceManager.GetObject("PPT_Small");
            pptImage = (Bitmap)image;
            pptImage.MakeTransparent(Color.White);
            codeImage = (Bitmap)Properties.Resources.ResourceManager.GetObject("Code_Small");
            codeImage.MakeTransparent(Color.White);
            videoImage = (Bitmap)Properties.Resources.ResourceManager.GetObject("Video_Small");
            videoImage.MakeTransparent(Color.White);
            qaImage = (Bitmap)Properties.Resources.qa.GetThumbnailImage(20, 20, null, IntPtr.Zero);
            qaImage.MakeTransparent(Color.White);
            //light image
            pptLightImage = (Bitmap)Properties.Resources.ResourceManager.GetObject("PPT_Light");
            pptLightImage.MakeTransparent(Color.White);
            codeLightImage = (Bitmap)Properties.Resources.ResourceManager.GetObject("Code_Light");
            codeLightImage.MakeTransparent(Color.White);
            videoLightImage = (Bitmap)Properties.Resources.ResourceManager.GetObject("Video_Light2");
            videoLightImage.MakeTransparent(Color.White);
            emptyImage = new Bitmap(110, 20, System.Drawing.Imaging.PixelFormat.Format8bppIndexed);
            emptyImage.MakeTransparent(Color.Black);
            qaLightImage = (Bitmap)Properties.Resources.lightqa.GetThumbnailImage(20, 20, null, IntPtr.Zero);
            qaLightImage.MakeTransparent(Color.White);
            //create an empty image
            completeImage = new Bitmap(110, 20, System.Drawing.Imaging.PixelFormat.Format64bppPArgb);
            //get graphics object from completeImage
            g = Graphics.FromImage(completeImage);
        }
        #endregion

        protected override object GetFormattedValue(object value,
                            int rowIndex, ref DataGridViewCellStyle cellStyle,
                            TypeConverter valueTypeConverter,
                            TypeConverter formattedValueTypeConverter,
                            DataGridViewDataErrorContexts context)
        {
                if (this.DataGridView.Rows.Count < rowIndex)
                { return emptyImage; }
                Course course = null;
                try
                {
                    course = this.DataGridView.Rows[rowIndex].DataBoundItem as Course;
                }
                catch
                {
                    return emptyImage;
                }
                if (course == null)
                {
                    return emptyImage;
                }
                g.Clear(Color.Transparent);
                //draw reminder
                if (Course.IsNewCourse(course))
                {
                    DrawNewCourse(course);
                }
                else
                {
                    DrawOldCourse(course);
                }
                return completeImage;
        }

        private void DrawOldCourse(Course course)
        {
            //get thre course
            CourseFileData codeData = course.Code;
            CourseFileData vidoeData = course.Video;
            CourseFileData pptData = course.PPT;
            CourseFileData qaData = course.QA;

            //draw propimage;
            //draw ppt
            if (pptData == null)
            {
                g.DrawImage(emptyImage, 0, 0);
            }
            else if (pptData.LifetimeStatue == LifetimePosition.DownloadProcessed)
                g.DrawImage(pptImage, 0, 0);
            else
                g.DrawImage(pptLightImage, 0, 0);
            //draw video
            if (vidoeData == null)
                g.DrawImage(emptyImage, 30, 0);
            else if (vidoeData.LifetimeStatue == LifetimePosition.DownloadProcessed)
                g.DrawImage(videoImage, 30, 0);
            else
                g.DrawImage(videoLightImage, 30, 0);
            //draw code
            if (codeData == null)
                g.DrawImage(emptyImage, 60, 0);
            else if (codeData.LifetimeStatue == LifetimePosition.DownloadProcessed)
                g.DrawImage(codeImage, 60, 0);
            else
                g.DrawImage(codeLightImage, 60, 0);
            //draw qa
            if (qaData == null)
                g.DrawImage(emptyImage, 90, 0);
            else if (qaData.LifetimeStatue == LifetimePosition.DownloadProcessed)
                g.DrawImage(qaImage, 90, 0);
            else
                g.DrawImage(qaLightImage, 90, 0);
        }

        private void DrawNewCourse(Course course)
        {
            if (Remind.RemindManager.IsCourseRegisted(course.Id))
            {
                g.DrawImage(reminderImage, 45, 0);
            }
            else if (Remind.RemindManager.CanRegister(course))
            {
                g.DrawImage(lightReminderImage, 45, 0);
            }
        }
    }
}